package winFrom

import (
	"fmt"

	"strconv"
	//"sync"
	"time"
)

func StartDownChann(mw *MyMainWindow) {

	timer2 := time.NewTicker(6 * time.Second)
	for {
		select {
		case <-timer2.C:
			mw.ButtonInt = mw.Urlmodel.RetUrlIsDownloding()
			if mw.ButtonInt == false {
				fmt.Println("mw.ButtonInt==false==退出")

			}
			startDown(mw)

		}
	}

	//chani := make(chan int, 10)
	//chnnquit := make(chan int, 10)
	//var cc int
	//go startDown(chani, chnnquit, mw)
	//for {
	//	select {
	//	case cc = <-chani:
	//		if cc != 0 {

	//			mw.Urlmodel.ResetRows()
	//		}
	//	case <-chnnquit:
	//		return
	//	}
	//}
}
func startDown(mw *MyMainWindow) {
	mw.ButtonInt = mw.Urlmodel.RetUrlIsDownloding()
	mw.Urlmodel.Num1 = strconv.Itoa(mw.Urlmodel.RetUrlDownlodingCount())
	if mw.ButtonInt == false {
		fmt.Println("mw.ButtonInt==false==")
		return
	}

	var countUrl_1, countUrl_0, countALL int

	for _, tb := range mw.Urlmodel.Tb_urls {
		if tb.isDownload == 1 {

			countUrl_1++

		}
		if tb.isDownload == 0 {

			countUrl_0++

		}
	}
	countALL = countUrl_0 + countUrl_1
	ii, _ := strconv.Atoi(mw.Urlmodel.Num1)
	fmt.Println("状态为正在执行的个数countUrl_1%d", countUrl_1)
	fmt.Println("状态为将开始执行的个数countUrl_0%d", countUrl_0)
	if ii <= countALL {

	}
	if ii > countALL {
		fmt.Println("ii--else%d", ii)
		mw.Urlmodel.ExecUrlListSql(`update tb_url set isDownload='0'
		  where isDownload='-1'and urlid in
		 ( select top ` + fmt.Sprintf("%d", ii-countALL) + ` urlid from  tb_url where isDownload='-1' )`)
		fmt.Println(`增加执行个数 select top ` + strconv.Itoa(ii-countALL) + ` urlid,url,isDownload,startTime,endTime
		 	from Tb_url where isDownload='0' `)
		mw.Urlmodel.Ini_Tb_url(`select top ` + strconv.Itoa(ii-countALL) + `  urlid,url,isDownload,startTime,endTime
		from Tb_url where isDownload='0' `)
		fmt.Println(mw.Urlmodel.Ini_Tb_url)

	}

	mw.Urlmodel.startDownload()

}
